/*
 *Copyright © 2015 zhaorongbao
 *招融宝
 *http://zhaorongbao.com
 *All rights reserved.
 */
package com.hanhai.common.util.biz;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hanhai.common.util.DateUtil;

public class ModelViewUtil {

	public static <T> Map<String,Object> getMapModelView(T bean,List<String> nameList){
		Map<String,Object> data = new HashMap<String,Object>();
		Class c = bean.getClass();
		
		for(String name:nameList){
			String first = name.substring(0, 1);
			String getMethodName  = "get"+name.replaceFirst(first, first.toUpperCase());
			try {
				Method m = c.getMethod(getMethodName);
				Object o=m.invoke(bean);
				data.put(name, o.toString());
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return data;
		
	}
	public static <T> List<Map<String,Object>> getMapModelViewList(List<T> beanList,List<String> nameList){
		
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		if(beanList==null||beanList.size()==0){
			return list;
		}
        Class c =beanList.get(0).getClass();
        Map<String,Method> mothodMap = new HashMap<String,Method>();
       

        for(String name:nameList){
			String first = name.substring(0, 1);
			String getMethodName  = "get"+name.replaceFirst(first, first.toUpperCase());
			try {
				Method m = c.getMethod(getMethodName);
				mothodMap.put(name, m);
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		for(T bean:beanList){
			Map<String,Object> data = new HashMap<String,Object>();
			list.add(data);
			for(Map.Entry<String, Method> entry:mothodMap.entrySet()){

				try {
					Object o=entry.getValue().invoke(bean);
					String viewStr = null;
					if(o!=null){
						if(o instanceof Date){
							viewStr = DateUtil.getDateTime(o);
						}else{
							viewStr = o.toString();
						}
					}
					
					data.put(entry.getKey(), viewStr);
					
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
		return list;
		
	}
	
	
	/**
	 * List<Object> 转 List<Map<String,Object>>,并可以指定需要的字段
	 * 
	 * @param beanList
	 * @param nameList
	 * @return
	 */
	public static <T> List<Map<String,Object>> getMapModelObjectList(List<T> beanList,List<String> nameList){
		
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		if(beanList==null||beanList.size()==0){
			return list;
		}
        Class c =beanList.get(0).getClass();
        Map<String,Method> mothodMap = new HashMap<String,Method>();
       

        for(String name:nameList){
			String first = name.substring(0, 1);
			String getMethodName  = "get"+name.replaceFirst(first, first.toUpperCase());
			try {
				Method m = c.getMethod(getMethodName);
				mothodMap.put(name, m);
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		for(T bean:beanList){
			Map<String,Object> data = new HashMap<String,Object>();
			list.add(data);
			for(Map.Entry<String, Method> entry:mothodMap.entrySet()){

				try {
					Object o=entry.getValue().invoke(bean);
					
					
					data.put(entry.getKey(), o);
					
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
		return list;
		
	}
	
	/*public static <T> List<Map<String,Object>> getMapModelObjectListByRemoveNameList(List<T> beanList,List<String> removeNameList){
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		if(beanList==null||beanList.size()==0){
			return list;
		}
        Class c =beanList.get(0).getClass();
        Map<String,Method> mothodMap = new HashMap<String,Method>();
       
        c.getFields();
        for(String name:nameList){
			String first = name.substring(0, 1);
			String getMethodName  = "get"+name.replaceFirst(first, first.toUpperCase());
			try {
				Method m = c.getDeclaredMethod(getMethodName);
				mothodMap.put(name, m);
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		for(T bean:beanList){
			Map<String,Object> data = new HashMap<String,Object>();
			list.add(data);
			for(Map.Entry<String, Method> entry:mothodMap.entrySet()){

				try {
					Object o=entry.getValue().invoke(bean);
					
					
					data.put(entry.getKey(), o);
					
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
		return list;
	}*/
	
}
